package com.edisoncgh.controller;

import com.edisoncgh.pojo.Manager;
import com.edisoncgh.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/managers")
public class ManagerController {
    @Autowired
    @Qualifier("ManagerServiceImpl")
    private ManagerService managerService;

    // 返回一个所有管理员的页面
    @RequestMapping("/allManagers")
    public String list(Model model) {
        List<Manager> list = managerService.findAllManager();
        model.addAttribute("list", list);
        return "allManagers";
    }

    @RequestMapping("/homepage")
    public String home() {
        return "homepage";
    }

    // 管理员登录
    @RequestMapping("/login")
    public String login(HttpServletRequest request, ModelMap map){
        if(request.getParameter("managerId") != null) {
            int managerId = Integer.parseInt(request.getParameter("managerId"));
            String managerPwd = request.getParameter("managerPwd");
            Manager manager = managerService.findManagerById(managerId);
            //检查是否存在该用户
            if (manager != null) {
                //检查密码
                if (!manager.getManagerPwd().equals(managerPwd)) {
                    map.addAttribute("loginStatus", "密码错误");
                    return "login";
                } else {
                    // return "redirect:/book/allBook";
                    // 修改session登录状态
                    request.getSession().setAttribute("status", "登录成功");
                    map.addAttribute("loginStatus", "登录成功");
                    return "redirect:homepage";
                }
            } else {
                map.addAttribute("loginStatus", "用户信息不存在");
                return "login";
            }
        } else {
            return "login";
        }
    }

//    @RequestMapping("loginout")
//    public String Loginout(HttpServletRequest request, ModelMap map){
//
//    }
}
